﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;

public partial class SystemManage_UpdatePassword : System.Web.UI.Page
{
    private string Code
    {
        get
        {
            return Share.UserValue(0);
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("100600");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("100600", "00017"))
        {
            Share.Alert(this.Page, "无权操作！");
            return;
        }
        Model.USERS user = new BLL.USERS().GetModel(this.Code);
        if (user != null)
        {
            if (FormsAuthentication.HashPasswordForStoringInConfigFile(txtOldPassword.Text.Trim(), "MD5").ToString() != user.USERS_PASSWORD)
            {
                Share.Alert(this.Page,"您输入的原始密码错误");
                return;
            }
            string newPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Text.Trim(), "MD5").ToString();
            if (new BLL.USERS().UpdateUserPassword(this.Code, newPassword))
            {
                Share.InsertLog("100600", "00002");
                Share.Alert(this.Page, "密码修改成功");
            }
            else
            {
                Share.Alert(this.Page, "密码修改失败");
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }

    public string EncryptPassword(string password)
    {
        MD5CryptoServiceProvider objMD5 = new MD5CryptoServiceProvider();
        return BitConverter.ToString(objMD5.ComputeHash(Convert2ByteArray(password)));
    }

    /// <summary>
    /// 将密码转换为字节数组
    /// </summary>
    /// <param name="password">密码</param>
    /// <returns></returns>
    private byte[] Convert2ByteArray(string password)
    {
        int intCount;
        char[] arrchar;

        arrchar = password.ToCharArray();
        byte[] arrByte = new byte[arrchar.Length];

        for (intCount = 0; intCount <= arrByte.Length - 1; intCount++)
        {
            arrByte[intCount] = Convert.ToByte(arrchar[intCount]);
        }

        return arrByte;
    }

}
